use std::time::SystemTime;
use chrono::{Local, NaiveDateTime, Duration};
use std::ops::{Sub, Add};

#[test]
pub fn date_demo1(){
    let now = SystemTime::now();
    let d = now.duration_since(SystemTime::UNIX_EPOCH).unwrap();
    println!("{}", d.as_millis()); //时间戳 毫秒
}

//使用chrono库 ,https://crates.io/crates/chrono
#[test]
pub fn date_demo2(){
    let now: NaiveDateTime = Local::now().naive_local();
    println!("{}", now);//2021-08-02 16:37:15.792087700

    println!("{}", now.timestamp_millis());//毫秒数

    //2021-08-02 16:39:49 格式化
    println!("{}", now.format("%Y-%m-%d %H:%M:%S").to_string());

    //解析
    let end_time = NaiveDateTime::parse_from_str("2020-03-06 18:36:27", "%Y-%m-%d %H:%M:%S").unwrap_or(now);
    println!("{}", end_time); //2020-03-06 18:36:27


}
//增减
#[test]
pub fn date_demo3(){
    let now = Local::now().naive_local();
    let d = now.sub(Duration::days(3));
    println!("{}", d);

    let d = now.add(Duration::days(-3));
    println!("{}", d);
}